package com.istudy.dao.impl;

import com.istudy.dao.BaseDao;
import com.istudy.dao.interfaces.UploadDao;
import com.istudy.entity.Upload;
import com.istudy.utils.Page;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 视频上传Dao实现类
 * @date 2020-09-01
 * @author GQ
 */
public class UploadDaoImpl  extends BaseDao implements UploadDao {
    /**
     * 获取展示列表
     *
     * @return list
     */
    @Override
    public List<Upload> getList(Page page) {
        List<Upload>list= new ArrayList<>();
        String sql="select * from upload ORDER BY `video_upload_time` LIMIT ?,4";
        List<Object> objects=new ArrayList<>();
        objects.add(page.getPageSize()*(page.getPageNo()-1));
        conn = getConnection();
        ResultSet resultSet = executeQuery(sql,objects.toArray());
        try {
            while ( resultSet.next()){
            Upload upload = new Upload();
            upload.setVideoId(resultSet.getInt("video_id"));
            upload.setVideoName(resultSet.getString("video_name"));
            upload.setVideoAddress(resultSet.getString("video_address"));
            upload.setVideoDescrtion(resultSet.getString("video_descrtion"));
            upload.setVideoUploadUser(resultSet.getString("video_upload_user"));
            upload.setVideoUploadTime(resultSet.getString("video_upload_time"));
            list.add(upload);
            }
             return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return null;
    }
    /**
     * 获取展示列表
     *
     * @return upload
     */
    @Override
    public Upload getUoload(int id) {
        String sql="select * from upload where `video_id`=?";
        List<Object> objects=new ArrayList<>();
        objects.add(id);
        conn = getConnection();
        ResultSet resultSet = executeQuery(sql,objects.toArray());
        Upload upload = null;
        try {
            while ( resultSet.next()){
                upload=new Upload();
                upload.setVideoId(resultSet.getInt("video_id"));
                upload.setVideoName(resultSet.getString("video_name"));
                upload.setVideoAddress(resultSet.getString("video_address"));
                upload.setVideoDescrtion(resultSet.getString("video_descrtion"));
                upload.setVideoUploadUser(resultSet.getString("video_upload_user"));
                upload.setVideoUploadTime(resultSet.getString("video_upload_time"));
                upload.setUserId(resultSet.getInt("user_id"));
            }
            return upload;
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return null;
    }
    /**
     * 添加上传记录
     *
     * @param upload 实体类
     * @return 受影响行数
     */
    @Override
    public int insertUpload(Upload upload) {

        String sql="insert into upload (`video_name`,`video_address`,`video_descrtion`,`video_upload_user`,`video_upload_time`,video_id)VALUE(?,?,?,?,?,?)";
        conn = getConnection();
        return executeUpdate(sql,new Object[]{
                upload.getVideoName(),
                upload.getVideoAddress(),
                upload.getVideoDescrtion(),
                upload.getVideoUploadUser(),
                upload.getVideoUploadTime(),
                upload.getVideoId()
        });
    }

    /**
     * 查询总记录数
     *
     * @return 总记录数
     */
    @Override
    public int getCount() {
        String sql="SELECT COUNT(1) FROM upload";
        BaseDao base=new BaseDao();
        ResultSet rs= base.executeQuery(sql);
        int count=0;
        try {
            if(rs.next()){
                count=rs.getInt("COUNT(1)");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 删除上传记录
     *
     * @param id id
     * @return 受影响行数
     */
    @Override
    public int deleteUpload(int id) {
        conn = getConnection();
        String sql="DELETE FROM upload  WHERE `video_id`=?";
        return executeUpdate(sql,new Object[]{
                id
        });
    }
}
